//
// Created by 26090 on 25-4-9.
//

#ifndef DM_IMU_H
#define DM_IMU_H

#include "bsp_can.h"
#include "stdint.h"
#include "daemon.h"

#define DM_IMU_MAX 2

#define ACCEL_CAN_MAX (58.8f)
#define ACCEL_CAN_MIN (-58.8f)
#define GYRO_CAN_MAX (34.88f)
#define GYRO_CAN_MIN (-34.88f)
#define PITCH_CAN_MAX (90.0f)
#define PITCH_CAN_MIN (-90.0f)
#define ROLL_CAN_MAX (180.0f)
#define ROLL_CAN_MIN (-180.0f)
#define YAW_CAN_MAX (180.0f)
#define YAW_CAN_MIN (-180.0f)
#define TEMP_MIN (0.0f)
#define TEMP_MAX (60.0f)
#define Quaternion_MIN (-1.0f)
#define Quaternion_MAX (1.0f)


typedef enum
{
    UPDATE_ACC = 0x01,
    UPDATE_GYRO = 0x02,
    UPDATE_ANGLE = 0x03,
    UPDATE_QUATERNION = 0x04,
}DM_IMU_Update_Type_e;

typedef struct
{
    uint8_t first_data;
    // 读取的值
    uint16_t acc_ecd[3];
    uint16_t gyro_ecd[3];
    uint16_t mag_ecd[3];
    uint16_t angle_ecd[3];
    uint16_t quaternion_ecd[4];

    // 计算值
    float accel[3];
    float gyro[3];
    float mag[3];
    float q[4];
    float roll;
    float pitch;
    float yaw;
    float yawTotalAngle;
    float yaw_offset;

    int16_t yawRoundCount;
    float yawAngleLast;
} DM_IMU_Measure_s;

typedef struct
{
    CAN_Init_Config_s can_init_config;
} DM_IMU_Init_Config_s;


typedef struct
{
    DM_IMU_Measure_s measure;

    CANInstance *can_instance; // IMUCAN实例，暂时只支持CAN
    DaemonInstance* daemon;
    uint32_t feed_cnt;
    float dt;

} DM_IMU_Instance;

DM_IMU_Instance *DMIMUInit(DM_IMU_Init_Config_s *config);
void DMIMURequestData(DM_IMU_Instance *instance,DM_IMU_Update_Type_e update_type);

#endif //DM_IMU_H
